package com.example.PolimiAssistant;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Window;
import com.example.PolimiAssistant.utils.AccountDataManager;
import com.example.PolimiAssistant.utils.UserDataManager;

/**
 * SplashScreen.java
 *
 * This is the entry point of the application. In here the applications decides
 * if the user will go to the main menu or to the login screen, by verifying the
 * existence of an account.
 */
public class SplashScreen extends Activity {

    /**
     * Log tag
     */
    private static final String TAG = SplashScreen.class.getSimpleName();

    /**
     * Declare the context of the application
     */
    private final Context context = this;

    /**
     * Time that the splash screen will last
     */
    private static int SPLASH_TIME_OUT = 3000;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        //1. Save the state of the activity
        super.onCreate(savedInstanceState);

        //2. Configure main display options

        //Avoid banding problems with the gradient background
        getWindow().setFormat(PixelFormat.RGBA_8888);

        // Set fixed portrait orientation
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        // Hide title bar
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        //3. Set the layout used for this activity
        setContentView(R.layout.splash_screen);


        //4. Extra configurations

        /*
         * This method allows the splash screen to last more time showing
         * the logo of the application. This method will be executed once
         * the timer is over.
         */
        new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {

                //1. Verify if the user has an account.
                boolean existAccount;
                existAccount = AccountDataManager.isThereAnAccount(context);

                //2. Send to login or main menu accordingly

                //If the user has an account
                if(existAccount){

                    Log.i(TAG, "An account previously existed, redirecting to Main Menu...");

                    //2.1 Create the intent to go to the
                    Intent goToMainMenu;
                    goToMainMenu = new Intent(context, MainMenu.class);

                    //2.2 Send the intent id to know where is it coming from
                    goToMainMenu.putExtra("COMING_FROM", context.getClass().getSimpleName());
                    startActivity(goToMainMenu);

                    //2.3 close this activity
                    finish();

                }else{

                    Log.i(TAG, "An account previously existed, redirecting to Login...");

                    //2.1 Create the intent to go to the
                    Intent goToLogin;
                    goToLogin = new Intent(SplashScreen.this, LoginScreen.class);

                    //2.2 Send the intent id to know where is it coming from
                    goToLogin.putExtra("COMING_FROM", context.getClass().getSimpleName());
                    startActivity(goToLogin);

                    //2.3 close this activity
                    finish();
                }

                //3. Declare the animations needed for the activity
                overridePendingTransition(R.anim.fade_in, R.anim.fade_out);

            }

        }, SPLASH_TIME_OUT);

        //5. Listeners

    }
}
